// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class LoadingScene extends cc.Component {

    @property(cc.Label)
    labelProgress: cc.Label = null;

    @property(cc.ProgressBar)
    progressBar: cc.ProgressBar = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        cc.director.preloadScene("GameScene", this.onProgress.bind(this), this.onLoaded.bind(this));
    }

    onProgress(completedCount: number, totalCount: number, item: any) {
        let progress = completedCount / totalCount;
        // this.progressBar.progress = progress;
        //添加动画效果
        cc.Tween.stopAllByTarget(this.progressBar);
        cc.tween(this.progressBar).to(0.2, { progress: progress }).start();

        console.log(Math.floor(progress * 100));

        this.labelProgress.string = "正在加载：" + Math.floor(progress * 100) + "%";
    }

    onLoaded(error: Error) {
        if (error) {
            console.log(error);
            return;
        }
        this.scheduleOnce(() => {
            cc.director.loadScene("GameScene");
        }, 0.8);
    }
}
